home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: Exceptions and operator new. Help
- Date: 16 Feb 1996 14:31:02 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4g24f6$54n@dawn.mmm.com>
- References: <4fu14a$2vg@news.tamu.edu>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Pradeep K Tapadiya (tpradeep@cs.tamu.edu) wrote:
- > According to the C++ specs, a new will throw a xalloc exception
- > if memory allocation fails. The return value from new need not be
- > NULL. In addition, the constructor can throw a different exception
- > as defined by you, the programmer. This means, each time, I do a
- > new, I have to do something like
-
- > myObject* p = NULL;
-
- > try {
- > myObject* p = new myObject;
- > }
- > catch (xlloc e) {
- > // do not delete p here since it was never allocated
- > ...
- > }
- > catch (myException& e) {
- > delete p;
- > ...
-
- This is not the case. If the constructor for myObject throws an exception,
- the memory allocated by new will be released for you.
-
- > Moreover, if myObject's destructor has to do some
- > memroy cleanup, for example,
-
- > myObject::~myObject ()
- > {
- > delete [] m_pVarList;
- > }
-
- > then, the constructor will probably has to do something like
-
- > myObject::myObject ()
- > {
- > try {
- > m_pVarList = new char [10];
- > }
- > catch (xalloc e) {
- > m_pVarList = NULL; // explicitly set it to NULL as destructor will
- > // try to delete a bogus pointer
- > throw xalloc ();
- > }
- > }
-
- If the constructor for myObject throws an exception, the myObject is
- not fully constructed, so its destructor will not be called. However,
- any fully constructed sub-objects of the myObject will be destroyed
- when an exception is thrown in the myObject constructor.
-
- > Now, setnewHandler adds another variable to the model. As you cannot
- > rely on what third party library is doing, anytime you do a new on
- > your object, you will explictly have to do a setnewhandler to your handler.
-
- Why will I have to do that?
-
- > Though we have been programming in C++ for quite some time, this is
- > the first time our company is venturing out into the world of
- > exceptions. It now appears exceptions would make the development process
- > more complicated.
-
- In some ways exceptions make the development process more complicated.
- In other ways they make it less complicated. They are a different way
- of reporting errors. IMHO, they offer greater potential for writing
- robust code, at the cost of some education on their use and pitfalls.
-
- > Am I missing something in the way I am using exceptions?
-
- Yes, but they're new to you.
-
- > Am I abusing exception handling mechanism?
-
- In the examples above, you're making it more difficult than it really is.
-
- > How are you handling exceptions with new opeartor?
-
- If you mean "what am I doing with out-of-memory conditions," I'm
- recovering the same way I always have. Exceptions don't change
- what I do to handle the error, they only change how I find out
- about it.
-
- If you mean "am I writing any special new handlers or extra
- try/catch blocks around constructors," the answer is no. The
- difference is that I no longer check for new returning 0, and
- I remember that when I call new, it may not return at all, but
- throw an exception.
-
- > How are you integrating your code with third party library/dll?
-
- Yes. The library, RogueWave Tools.h++, will throw exceptions.
- I find it refreshing to write code that does not explicitly check
- for errors. But I do have to remember that exceptions can be thrown
- nearly any time, and write my code accordingly. This is the hard
- part about using exceptions.
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-